use "created_dta\analysis_samp", clear

egen qtr = group(year_arrival quarter_arrival)
g last_useable_date = ym(2019,12) 
g last_useable_date_ssa = ym(2018,9)
g last_useable_date_credit = ym(2017,12)
g months_observed =  last_useable_date -ym(year_arrival,month_arrival) 
g months_observed_ssa = last_useable_date_ssa -ym(year_arrival,month_arrival)
g months_observed_credit = last_useable_date_credit -ym(year_arrival,month_arrival)
g num_qtr_allowed =  floor((months_observed)/3)
g num_qtr_allowed_ssa = floor((months_observed_ssa/3))
g num_qtr_allowed_credit = floor((months_observed_credit/3))
 
egen mos_post_qtr_terms = group(init_pmos_cons post_orgraa year_arrival quarter_arrival init_terms)
egen mos_post_qtr_terms_bct = group(init_pmos_cons post_orgraa year_arrival quarter_arrival init_terms bct_type)
egen mth = group(year_arrival month_arrival)

bys dmsl_tc_first qtr : egen meandep = mean(dep_win_3yr)
bys dmsl_tc_first qtr: egen ct = total(1)
g insamp_inst_3_yr = ((meandep*ct) - dep_win_3yr)/(ct-1)


forvalues x = 1(1)36 {
	local w1 = 3*(`x'-1) 
	local w2 = 3*(`x'-1) + 1 
	local w3 = 3*(`x'-1) + 2 

g deployed_q`x' = (deployed_`w1' == 1 | deployed_`w2' == 1 | deployed_`w3' == 1)
}

forvalues x = 1(1)$endx {
   g sep_by_`x' = dur_army >= 0 &  dur_army < 0.25*(`x')
   g sep_in_`x' = dur_army >= 0.25*(`x'-1) & dur_army < 0.25*(`x') 
}

g ym_arr = ym(year_arrival,month_arrival) 
cap drop _merge


gen n = 1
merge m:1 n using raw_dta\cpi_wide, nogen
drop n 

merge 1:1 pid using raw_dta/VADCpanel_cleaned_wide, keep(1 3) nogen
*ALREDAY ADJUSTED FOR INFLATION INTO 2020 DOLLARS
forvalues x = 1(1)56 {
*any vadc 
    gen anyvadc_in_`x' = any_vadc2004 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2005(1)2018 {
replace  anyvadc_in_`x' = any_vadc`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace anyvadc_in_`x' = any_vadc2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace anyvadc_in_`x' = 0 if anyvadc_in_`x' == .

*vadc amt 
    gen amtvadc_in_`x' = vadc_gross_amt2004 if (ym_arr + 3*(`x'-1)) >= ym(2004,9) 
	forvalues yy = 2005(1)2018 {
replace  amtvadc_in_`x' = vadc_gross_amt`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
replace amtvadc_in_`x' = vadc_gross_amt2019 if (ym_arr + 3*(`x'-1)) >= ym(2019,9)
replace amtvadc_in_`x' = 0 if amtvadc_in_`x' == .

}


drop any_vadc* vadc_gross_amt* vadc_worklim* any_ptsd* any_tbi* vadc*

*merge in casualty data
merge 1:1 pid using raw_dta\cas_unit_deploy_nopii_wide_v3, nogen keep(1 3)

gen cas_death_myr = incident_myr1 if death1 == 1
gen cas_death_country = incident_country1 if death1 == 1

forvalues i = 1/5 {
	gen months_to_incident`i' = incident_myr`i' - ym_arr
	replace cas_death_myr = incident_myr`i' if death`i' == 1
	replace cas_death_country = incident_country`i' if death1 == `i'
}

forvalues x = 1(1)$endx {
	gen wia_by_`x' = months_to_incident1 >= 0 &  months_to_incident1 < 3*(`x') & wia1==1
	gen kia_by_`x' = months_to_incident1 >= 0 &  months_to_incident1 < 3*(`x') & kia1==1
	
	forvalues y = 2/5 {
		replace wia_by_`x' =1 if months_to_incident`y' >= 0 &  months_to_incident`y' < 3*(`x') & wia`y'==1
		replace kia_by_`x' =1 if months_to_incident`y' >= 0 &  months_to_incident`y' < 3*(`x') & kia`y'==1
	}
	
}


*NDI Death Data 
merge 1:1 pid using "created_dta\ndi_death_data_clean_v2", nogen keep(1 3) keepusing(ndi_death_myr ndi_suicide* ndi_assault* ndi_war* ndi_all_acc ndi_firearm* ndi_drug* ndi_narcotic* ndi_alcohol* ndi_mv*  ndi_other ndi_other_2 ndi_und_intent* ndi_despair* ndi_combat*)

format ndi_death_myr %tm

gen months_to_death = ndi_death_myr - ym_arr

local CAUSE despair drug drug_acc drug_self drug_und narcotic narcotic_self narcotic_und alcohol alcohol_acc alcohol_self alcohol_und firearm firearm_acc firearm_self firearm_und  suicide suicide_plus mv mv_acc mv_self mv_und war assault und_intent other combat
forvalues x = 1(1)$endx {
    gen ndi_death_by_`x' = months_to_death >= 0 &  months_to_death < 3*(`x')
	foreach cause in `CAUSE' {
	    gen ndi_`cause'_by_`x' = ndi_death_by_`x' & ndi_`cause'
		gen ndi_`cause'2_by_`x' = ndi_death_by_`x' & ndi_`cause'_2
	}
	gen combat_death_by_`x' = ndi_combat_by_`x' == 1 | kia_by_`x' == 1
} 

drop months_to_death 

drop months_to_incident? incident* wia? kia? suicide? death? serwia? vserwia? any_wia any_kia any_vserwia any_wia  compt?
drop ndi_suicide ndi_suicide_plus ndi_suicide_2 ndi_suicide_plus_2 ndi_drug ndi_drug_acc ndi_drug_self ndi_drug_und ndi_drug_2 ndi_drug_acc_2 ndi_drug_self_2 ndi_drug_und_2 ndi_narcotic ndi_narcotic_acc ndi_narcotic_self ndi_narcotic_und ndi_narcotic_2 ndi_narcotic_acc_2 ndi_narcotic_self_2 ndi_narcotic_und_2 ndi_alcohol ndi_alcohol_acc ndi_alcohol_self ndi_alcohol_und ndi_alcohol_2 ndi_alcohol_acc_2 ndi_alcohol_self_2 ndi_alcohol_und_2 ndi_firearm ndi_firearm_acc ndi_firearm_self ndi_firearm_und ndi_firearm_2 ndi_firearm_acc_2 ndi_firearm_self_2 ndi_firearm_und_2  ndi_mv ndi_mv_acc ndi_mv_self ndi_mv_und ndi_mv_2 ndi_mv_acc_2 ndi_mv_self_2 ndi_mv_und_2 ndi_war_2 ndi_assault ndi_assault_2 ndi_und_intent ndi_und_intent_2 ndi_other ndi_other_2 ndi_despair ndi_despair_2

**# Merge ssdi/ssi
merge 1:1 pid using raw_dta\ssa_death_ssi_unit_deploy_nopii, keep(1 3) nogen
merge 1:1 pid using raw_dta\ssdi_unit_deploy_cleaned_nopii, keep(1 3) nogen

forvalues x = 1/$endx {
	g anyssdi_in_`x' = ANY_DIB1985 if (ym_arr + 3*(`x'-1)) >= ym(1985,9)
	forvalues yy = 1986/2018 {
		replace anyssdi_in_`x' = ANY_DIB`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
	replace anyssdi_in_`x' = 0 if anyssdi_in_`x' == .
}

forvalues y = 1985/2019 {
	local yy = min(`y',2017)
		replace DIB_TOT`y' = DIB_TOT`y'*(258.844/cpiaucsl`yy')
}
forvalues x = 1/$endx {
	g amtssdi_in_`x' = DIB_TOT1985 if (ym_arr + 3*(`x'-1)) >= ym(1985,9)
	forvalues yy = 1986/2018 {
		replace amtssdi_in_`x' = DIB_TOT`yy' if (ym_arr + 3*(`x'-1)) >= ym(`yy',9)
	}
	replace amtssdi_in_`x' = 0 if amtssdi_in_`x' == . 
}

gen amtssdi_in_2018 = DIB_TOT2018
replace amtssdi_in_2018 = 0 if missing(DIB_TOT2018)
gen amtssdi_in_2019 = DIB_TOT2019
replace amtssdi_in_2019 = 0 if missing(DIB_TOT2019)

drop ANY_DIB* DIB_TOT*

forvalues x = 133/538 {
	gen any_ssi`x' = 1 if (FEDPMT_`x' > 0 & FEDPMT_`x' != .) | (STATPMT_`x' >0 & STATPMT_`x' !=.)
	gen amt_ssi`x' = FEDPMT_`x' + STATPMT_`x'
	local y = ceil(`x'/12) + 1973
	local yy = min(`y',2017) //2018 and 2019 are imputed using 2017 and have not been adjusted for inflation yet (cpi2019/cpi2017*cpi202/cpi2019 = cpi202/cpi2017)
	replace amt_ssi`x' = amt_ssi`x'*(258.844/cpiaucsl`yy')
}
forvalues q = 1/135 {
	local m = 3*`q'+130
	local n = `m' + 1
	local j = `m' + 2
	gen any_ssiq`q' = 1 if any_ssi`m' == 1 | any_ssi`n' == 1 | any_ssi`j' == 1
	gen amt_ssiq`q' = amt_ssi`m' + amt_ssi`n' + amt_ssi`j'
}
forvalues x = 1/$endx {
	gen anyssi_in_`x' = any_ssiq1 if (ym_arr + 3*(`x'-1)) >= ym(1985,1)
	gen amtssi_in_`x' = amt_ssiq1 if (ym_arr + 3*(`x'-1)) >= ym(1985,1)
	forvalues q = 2/135 {
		replace anyssi_in_`x' =  any_ssiq`q' if (ym_arr + 3*(`x'-1)) >= (3*`q'+297)
		replace amtssi_in_`x' =  amt_ssiq`q' if (ym_arr + 3*(`x'-1)) >= (3*`q'+297)
	}
	replace anyssi_in_`x' = 0 if anyssi_in_`x' == .
	replace amtssi_in_`x' = 0 if amtssi_in_`x' == .
}

g amtssi_in_2018 = amt_ssi526 + amt_ssi527 + amt_ssi528 + amt_ssi529 + amt_ssi530 + amt_ssi531 + amt_ssi532 + amt_ssi533 + amt_ssi534 + amt_ssi535 + amt_ssi536 + amt_ssi537 
replace amtssi_in_2018 = 0 if missing(amtssi_in_2018)

g amtssi_in_2019 = amtssi_in_2018

drop FEDPMT_* STATPMT_* PSTAT_* any_ssi* amt_ssi* 

*Disability Outcomes
forvalues x = 1/$endx { 
	gen anydisability_in_`x' = 0
	replace anydisability_in_`x' = 1 if anyssdi_in_`x' == 1 | anyssi_in_`x' == 1
	if `x' < 57 replace anydisability_in_`x' = 1 if anyvadc_in_`x' == 1
	
	
	gen amtdisability_in_`x' = amtssdi_in_`x' + 4*amtssi_in_`x' 
	if `x' < 57 replace amtdisability_in_`x' = amtdisability_in_`x' + amtvadc_in_`x'
	replace amtdisability_in_`x' = 0 if amtdisability_in_`x' == .
}


*Separation codes 
merge 1:1 pid using raw_dta/spd_spell_panel, keep(1 3) nogen
summ ym_of_sep1 last_ym if dur_army<2.5

forvalues y = 1(1)9 {
g months_to_sep`y' = ym_of_sep`y' - ym_arr
}

tab months_to_sep1 if year_arrival == 2015 & month_arrival == 12 
tab months_to_sep2 if year_arrival == 2014 & month_arrival == 12

forvalues x = 1(1)$endx {
   g misconductsep_by_`x' =  months_to_sep1 >= 0 &  months_to_sep1 < 3*(`x') & (inlist(spd1,"KFS","JHJ","941","942") | inlist(substr(spd1,1,2),"JJ","JK","JP"))
   g misconductsep_in_`x' =  months_to_sep1 >= 3*(`x'-1) &  months_to_sep1 < 3*(`x') & (inlist(spd1,"KFS","JHJ","941","942") | inlist(substr(spd1,1,2),"JJ","JK","JP"))
    forvalues y = 2(1)9 {
	replace misconductsep_by_`x' = 1 if months_to_sep`y' >= 0 &  months_to_sep`y' < 3*(`x') & (inlist(spd`y',"KFS","JHJ","941","942") | inlist(substr(spd`y',1,2),"JJ","JK","JP"))
	replace misconductsep_in_`x' = 1 if months_to_sep`y' >= 3*(`x'-1) &  months_to_sep`y' < 3*(`x') & (inlist(spd`y',"KFS","JHJ","941","942") | inlist(substr(spd`y',1,2),"JJ","JK","JP"))
	}
}

forvalues x = 1(1)$endx {
   g bar_by_`x' =  months_to_sep1 >= 0 &  months_to_sep1 < 3*(`x') & inlist(spd1,"JBK","LBK","JCC","LCC","JGH","LGH","KGF")
    g bar_in_`x' =  months_to_sep1 >= 3*(`x'-1) &  months_to_sep1 < 3*(`x') & inlist(spd1,"JBK","LBK","JCC","LCC","JGH","LGH","KGF")
    forvalues y = 2(1)9 {
	replace bar_by_`x' = 1 if months_to_sep`y' >= 0 &  months_to_sep`y' < 3*(`x') & inlist(spd`y',"JBK","LBK","JCC","LCC","JGH","LGH","KGF")
	replace bar_in_`x' = 1 if months_to_sep`y' >= 3*(`x'-1) &  months_to_sep`y' < 3*(`x') & inlist(spd`y',"JBK","LBK","JCC","LCC","JGH","LGH","KGF")
	}
}

forvalues x = 1(1)$endx {
   g bar_or_misconduct_by_`x' = 1 if misconductsep_by_`x' == 1 | bar_by_`x' == 1
   replace bar_or_misconduct_by_`x' = 0 if bar_or_misconduct_by_`x' == .
   g bar_or_misconduct_in_`x' = 1 if misconductsep_in_`x' == 1 | bar_in_`x' == 1
   replace bar_or_misconduct_in_`x' = 0 if bar_or_misconduct_in_`x' == .
}

drop ym_of_sep? spdgrp? spd?

merge 1:1 pid using raw_dta\clean_incarcerations_wide, nogen keep(1 3)

g ym_incarceration = ym(year(incarceration_dt),month(incarceration_dt))
g months_to_incarceration = ym_incarceration - ym_arr
forvalues y = 1/9 {
	g ym_release`y' = ym(year(release_dt`y'),month(release_dt`y'))
	g months_to_release`y' = ym_release`y' - ym_arr
}
count if months_to_incarceration < 0
forvalues qtr = 1/$endx {
	g incarcerated_by_`qtr' = months_to_incarceration >= 0 & months_to_incarceration < 3*(`qtr')
	g incarcerated_in_`qtr' = months_to_incarceration >= 3*(`qtr'-1) & months_to_incarceration < 3*(`qtr')
	forvalues y = 2/9 {
		replace incarcerated_in_`qtr' = months_to_incarceration >= 3*(`qtr'-1) & months_to_incarceration < 3*(`qtr')
	}
}

*Merge Credit Data
merge 1:1 pid using raw_dta/credit_coverage_2003_2017_wide_v2, keep(1 3) gen(_merge_credit_coverage)
replace credit_input_all = 0 if missing(credit_input_all)
replace credit_input_record = 0 if missing(credit_input_record)
foreach x in input_file_2015_F00100 input_file_2015_F00135 input_file_2016 input_file_2017 input_file_2018 {
	replace `x' = 0 if `x' == .
}
merge 1:1 pid using raw_dta/credit_outcomes_2003_2017_v2_wide, keep(1 3) gen(_merge_credit_outcomes)

foreach m in june dec {
	if "`m'" == "june" local mth = 6 
	if "`m'" == "dec" local mth = 12 
	foreach y in 2003 2004 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 {
		local ym = ym(`y',`mth')
		rename extract_coverage_`m'`y' extract_coverage_`ym'
		rename in_credit_samp_`m'`y' in_credit_samp_`ym'
	}
}

gen vantage_2007_2017 = (vantage_v3_score557 !=. | vantage_v3_score563 !=.) & (vantage_v3_score569 !=. | vantage_v3_score575 !=.) & (vantage_v3_score581 !=. | vantage_v3_score587 !=.) & (vantage_v3_score593 !=. | vantage_v3_score599 !=.) & (vantage_v3_score605 !=. | vantage_v3_score611 !=.) & (vantage_v3_score617 !=. | vantage_v3_score623 !=.) & (vantage_v3_score629 !=. | vantage_v3_score635 !=.) & (vantage_v3_score641 !=. | vantage_v3_score647 !=.) & (vantage_v3_score653 !=. | vantage_v3_score659 !=.) & (vantage_v3_score665 !=. | vantage_v3_score671 !=.) & (vantage_v3_score677 !=. | vantage_v3_score683 !=.) & (vantage_v3_score689 !=. | vantage_v3_score695 !=.)

g any_vantage = 0
foreach z in 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace any_vantage = 1 if vantage_v3_score`z' != .
}

foreach x in vantage_score fico_score vantage_v3_score ficeclv9_score premier_v1_2_all5020 premier_v1_2_aua5020 premier_v1_2_bca5020 premier_v1_2_all5070 premier_v1_2_all5820 premier_v1_2_stu5020 premier_v1_2_stu5820 premier_v1_2_aua5820 premier_v1_2_col5060 premier_v1_2_mtf5820 premier_v1_2_mts5820 premier_v1_2_hlc5620 premier_v1_2_mtf5020 premier_v1_2_all2870 premier_v1_2_all2875 {
	foreach z in 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		if "`x'" != "vantage_v3_score" replace `x'`z' = 0 if in_credit_samp_`z' == 1 & `x'`z' == .
		gen any_`x'`z' = `x'`z' > 0 if `x'`z' != .
	}
forvalues y = 1(1)$endx {
	gen `x'_in_`y' = `x'521 if (ym_arr + 3*(`y'-1)) >= 521
	foreach z in 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		replace `x'_in_`y' = `x'`z' if (ym_arr + 3*(`y'-1)) >= `z'
	}
}
}

*Gen credit coverage variables
forvalues y = 1(1)$endx {
	gen credit_coverage_in_`y' = extract_coverage_521 if (ym_arr + 3*(`y'-1)) >= 521
	g in_credit_samp_in_`y' = in_credit_samp_521 if (ym_arr + 3*(`y'-1)) >= 521
	foreach z in 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		replace credit_coverage_in_`y' = extract_coverage_`z' if (ym_arr + 3*(`y'-1)) >= `z'
		replace in_credit_samp_in_`y' = in_credit_samp_`z' if (ym_arr + 3*(`y'-1)) >= `z'
	}
	replace credit_coverage_in_`y' = 0 if credit_coverage_in_`y' == .
}

*Calculate credit score `Y' yrs after arrival based on closest nonmissing credit score within 6 months of ym_arr 12*`Y' 
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	g mnths_to_m`z' = abs(ym_arr-`z')
}
forvalues x = 1/2 {
	g credit_ym`x' = .
	g mnths_to_credit_ym`x' = .
}
	*Find to closet credit score months within 6 months of arrival
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym1 = `z'  if mnths_to_m`z' <=6  & mnths_to_m`z' <= mnths_to_credit_ym1
	replace mnths_to_credit_ym1 = mnths_to_m`z' if mnths_to_m`z' <=6  & mnths_to_m`z' <= mnths_to_credit_ym1
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym2 = `z'  if mnths_to_m`z' <=6  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1
	replace mnths_to_credit_ym2 = mnths_to_m`z' if mnths_to_m`z' <=6  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1
}
	
forvalues y = 1(1)10 {
	g vantage_6mo_score_in_yr`y' = .
	g in_credit_samp_6mo_in_yr`y' = .
	foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		replace vantage_6mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym1 + 12*`y'
		replace vantage_6mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym2 + 12*`y' & vantage_6mo_score_in_yr`y' == .
		replace in_credit_samp_6mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym1 + 12*`y'
		replace in_credit_samp_6mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym2 + 12*`y' & in_credit_samp_6mo_in_yr`y' != 1
	}
}

drop credit_ym* mnths_to_credit_ym*
*Repeat for credit scores within 18 months
forvalues x = 1/6 {
	g credit_ym`x' = .
	g mnths_to_credit_ym`x' = .
}

foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym1 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym1
	replace mnths_to_credit_ym1 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym1
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym2 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1 
	replace mnths_to_credit_ym2 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym3 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym3 & `z' != credit_ym1 & `z' != credit_ym2
	replace mnths_to_credit_ym3 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym3 & `z' != credit_ym1 & `z' != credit_ym2
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym4 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym4 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3
	replace mnths_to_credit_ym4 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym4 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym5 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym5 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4
	replace mnths_to_credit_ym5 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym5 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym6 = `z'  if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym6 = mnths_to_m`z' if mnths_to_m`z' <=18  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
}


forvalues y = 1(1)10 {
	g vantage_18mo_score_in_yr`y' = .
	g in_credit_samp_18mo_in_yr`y' = .
	foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		replace vantage_18mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym1 + 12*`y'
		replace in_credit_samp_18mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym1 + 12*`y'

	}
		forvalues x = 2/6 {
			foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {	
				replace vantage_18mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym`x' + 12*`y' & vantage_18mo_score_in_yr`y' == .
				replace in_credit_samp_18mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym`x' + 12*`y' & in_credit_samp_18mo_in_yr`y' != 1

		}
	}
}

drop credit_ym* mnths_to_credit_ym*
*Repeat for credit scores within 30 months
forvalues x = 1/10 {
	g credit_ym`x' = .
	g mnths_to_credit_ym`x' = .
}

foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym1 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym1
	replace mnths_to_credit_ym1 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym1
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym2 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1 
	replace mnths_to_credit_ym2 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym2 & `z' != credit_ym1
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym3 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym3 & `z' != credit_ym1 & `z' != credit_ym2
	replace mnths_to_credit_ym3 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym3 & `z' != credit_ym1 & `z' != credit_ym2
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym4 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym4 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3
	replace mnths_to_credit_ym4 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym4 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym5 = `z'  if mnths_to_m`z' <=30 & mnths_to_m`z' <= mnths_to_credit_ym5 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4
	replace mnths_to_credit_ym5 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym5 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym6 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym6 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym7 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym7 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5  & `z' != credit_ym6 
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym8 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym8 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5  & `z' != credit_ym6 & `z' != credit_ym7 
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym9 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym9 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5  & `z' != credit_ym6 & `z' != credit_ym7 & `z' != credit_ym8
}
foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
	replace credit_ym10 = `z'  if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5
	replace mnths_to_credit_ym10 = mnths_to_m`z' if mnths_to_m`z' <=30  & mnths_to_m`z' <= mnths_to_credit_ym6 & `z' != credit_ym1 & `z' != credit_ym2 & `z' != credit_ym3 & `z' != credit_ym4 & `z' != credit_ym5  & `z' != credit_ym6 & `z' != credit_ym7 & `z' != credit_ym8 & `z' != credit_ym9
}

forvalues y = 1(1)10 {
	g vantage_30mo_score_in_yr`y' = .
	g in_credit_samp_30mo_in_yr`y' = .
	foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {
		replace vantage_30mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym1 + 12*`y'
		replace in_credit_samp_30mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym1 + 12*`y'

	}
		forvalues x = 2/10 {
			foreach z in 521 527 533 539 557 563 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 695 {	
				replace vantage_30mo_score_in_yr`y' = vantage_v3_score`z' if `z' == credit_ym`x' + 12*`y' & vantage_30mo_score_in_yr`y' == .
				replace in_credit_samp_30mo_in_yr`y' = in_credit_samp_`z' if `z' == credit_ym`x' + 12*`y' & in_credit_samp_30mo_in_yr`y' != 1

		}
	}
}

drop ficeclv9_score??? extract_coverage_??? in_credit_samp_??? credit_ym? mnths_to_credit_ym? mnths_to_m??? 

foreach z in 569 575 581 587 593 599 605 611 617 623 629 635 641 647 653 659 665 671 677 683 689 {
	local input 
	if inlist(`z',569, 581, 593, 605, 617, 629, 641, 653) {
		local input input_file_2016_F00135
	}
	if inlist(`z',575, 587, 599, 611, 623, 635, 647, 659, 689) {
		local input input_file_2015_F00100 == 1 | input_file_2015_F00135 == 1
	}
	if inlist(`z',665, 671, 677, 683) {
		local input input_file_2017_F00000 == 1
	}
	if inlist(`z',587,599) {
		local input input_file_2015_F00100 == 1 | input_file_2015_F00135 == 1 | input_file_2017_F00000 == 1
	}
	if inlist(`z',581,599) {3
		local input input_file_2016_F00135 == 1 | input_file_2017_F00000 == 1
	}

	g no_credit_score`z' = vantage_score`z' != 1 & (`input') 
	g no_credit2_score`z' = vantage_score`z' != 1 if (`input') 
	g miss_credit_score`z' = vantage_score`z' != 1 & !(`input')
	g miss_credit2_score`z'= vantage_score`z' != 1
}
*ADD Dec 2020 Credit Outcomes
merge 1:1 pid using raw_dta/credit_extract_202012_v2_nopii, nogen keep(1 3)

foreach x in p13_all5020_dec2020 p13_all5070_dec2020 p13_col5060_dec2020 p13_aua5020_dec2020 p13_bca5020_dec2020 p13_stu5020_dec2020 p13_all5820_dec2020 p13_aua5820_dec2020 p13_mtf5820_dec2020 p13_mts5820_dec2020 p13_stu5820_dec2020 p13_mtf5020_dec2020 p13_all2870_dec2020 p13_all2875_dec2020 {
	replace `x' = . if `x' >= 999999990
	replace `x' = 0 if `x' == . & _merge_in_unit_deploy_iv == 3
	g any_`x' = `x' > 0 & `x' != .
}
foreach x in p13_all5020 p13_all5070 p13_col5060 p13_aua5020 p13_bca5020 p13_stu5020 p13_all5820 p13_aua5820 p13_mtf5820 p13_mts5820 p13_stu5820 p13_mtf5020 p13_all2870 p13_all2875  {
	local ym = ym(2020,12)
	rename `x'_dec2020 `x'`ym'
	rename any_`x'_dec2020 any_`x'`ym'
}

foreach x in vantage_v4_score fictbv10_score {
local ym = ym(2020,12)
replace `x'_dec2020 = . if `x'_dec2020 < 300 | `x'_dec2020 > 850
g any_`x'`ym' = `x'_dec2020 != . if _merge_in_unit_deploy_iv == 3
g no_`x'`ym' = `x'_dec2020 == .
rename `x'_dec2020 `x'`ym'
}
g any_credit_file731 = _merge_out_unit_deploy_iv == 3


*Merge in army profile data
merge 1:1 pid using raw_dta/analysis_sample_phypr_cleaned.dta, keep(1 3) keepusing(any_*profile_in_* any_*phys_in_* any_*hearing_in_* any_*psychiatric_in_* any_*profile_by_* any_*phys_by_* any_*hearing_by_* any_*psychiatric_by_* phypr_in_0 any_*othprof_in_* any_*othprof_by_*) nogen

*LN
g init_entry_yr = year(init_entrydt_s)
g init_entry_mth = month(init_entrydt_s)
g init_entry_fy = init_entry_yr
replace init_entry_fy = init_entry_yr + 1 if inrange(init_entry_mth,10,12)

g ln_yr_restriction = !inrange(init_entry_yr,2002,2015) & !inrange(init_entry_fy,2002,2015) // 11 Obs

	preserve
	use raw_dta/ln_pid_xwalk, clear
	keep if  pid_unit_deploy!= . 
	tempfile sent
	save `sent', replace
	restore

g pid_unit_deploy = pid
merge 1:1 pid_unit_deploy using  `sent', gen(_merge_ln_samp) keep(1 3) keepusing(pid_unit_deploy pid_ln)
g didnt_send_to_ln = _merge_ln_samp == 1 
drop pid_unit_deploy

tab didnt_send_to_ln 

g ln_samp = ln_yr_restriction == 0 & didnt_send_to_ln == 0
tab ln_samp // 99.25% of analysis sample is in LN sample

gen post_state = post_orgraa
replace post_state = "TX" if post_state=="Fort Bliss" | post_state=="Fort Hood"
replace post_state = "NC" if post_state=="Fort Bragg"
replace post_state = "KY" if post_state=="Fort Campbell"
replace post_state = "CO" if post_state=="Fort Carson"
replace post_state = "WA" if post_state=="Fort Lewis"
replace post_state = "NY" if post_state=="Fort Drum"
replace post_state = "KS" if post_state=="Fort Riley"
replace post_state = "GA" if post_state=="Fort Stewart"
replace post_state = "HI" if post_state=="Schofield Barracks"
 
* Merge Incarceration Data
merge 1:1 pid using "raw_dta/inc_data.dta",  keepusing(inc_by_* pre_inc) gen(_merge_inc_ln) keep(1 3)

foreach var of varlist inc_by_* pre_inc {
	replace `var' = 0 if `var'==. & ln_samp == 1
	}


*MERGE NSC
merge 1:1 pid using raw_dta/unit_deploy_nsc_graduations_v5_wide_nopii.dta, nogen keep(1 3)
merge 1:1 pid using raw_dta/unit_deploy_nsc_enrollments_v5_wide_nopii.dta, nogen keep(1 3)

forvalues y = 2000/2021 {
	gen nsc_enroll_fy`y' = enroll_fy1 == `y'
	forvalues x = 2/25 {
		replace nsc_enroll_fy`y' = 1 if enroll_fy`x' == `y'
	}
}
forvalues x = 0/$endx {
	g nsc_enroll_in_`x' = nsc_enroll_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9) 
	g nsc_enroll_by_`x' = nsc_enroll_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9) 
	g nsc_enr_postarr_by_`x' = nsc_enroll_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9) & ym_arr < ym(2000,9)
	
	forvalues y = 2001/2021 {
		replace nsc_enroll_in_`x' = nsc_enroll_fy`y' if (ym_arr + 3*(`x'-1)) >= ym(`y',9)
		replace nsc_enroll_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_enroll_fy`y' == 1 
		
		replace nsc_enr_postarr_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_enroll_fy`y' == 1 & ym_arr < ym(`y',9)

	}
	replace nsc_enr_postarr_by_`x' = 0 if missing(nsc_enr_postarr_by_`x')
	
}
forvalues x = 1/4 {
	g nsc_enroll_in_m`x' = nsc_enroll_fy2000 if (ym_arr + 3*(-`x'-1)) >= ym(2000,9) 
	g nsc_enroll_by_m`x' = nsc_enroll_fy2000 if (ym_arr + 3*(-`x'-1)) >= ym(2000,9) 
	
	forvalues y = 2001/2021 {
		replace nsc_enroll_in_m`x' = nsc_enroll_fy`y' if (ym_arr + 3*(-`x'-1)) >= ym(`y',9)
		replace nsc_enroll_by_m`x' = 1 if (ym_arr + 3*(-`x'-1)) >= ym(`y',9) & nsc_enroll_fy`y' == 1 
		
	}
}

gen nsc_enroll_by_2017 = nsc_enroll_fy2000
gen nsc_enroll_by_2020 = nsc_enroll_fy2000
gen nsc_enr_pa_by_2017 = nsc_enroll_fy2000 if ym_arr < ym(2000,9)
gen nsc_enr_pa_by_2020 = nsc_enroll_fy2000 if ym_arr < ym(2000,9)

forvalues y = 2001/2017 {
	replace nsc_enroll_by_2017 = 1 if nsc_enroll_fy`y' ==1
	replace nsc_enr_pa_by_2017 = 1 if nsc_enroll_fy`y' ==1 & ym_arr < ym(`y',9)
	}
forvalues y = 2001/2020 {
	replace nsc_enroll_by_2020 = 1 if nsc_enroll_fy`y' ==1 	
	replace nsc_enr_pa_by_2020 = 1 if nsc_enroll_fy`y' ==1 & ym_arr < ym(`y',9)
}

replace nsc_enr_pa_by_2017 = 0 if missing(nsc_enr_pa_by_2017)
replace nsc_enr_pa_by_2020 = 0 if missing(nsc_enr_pa_by_2020)

drop nsc_enroll_fy* enroll_fy*

forvalues y = 2000/2021 {
	gen nsc_grad_fy`y' = grad_fy1 == `y'
	foreach deg in assc bach mstr dctr {
		gen nsc_`deg'_fy`y' = `deg'1 if grad_fy1 == `y'
	}
	forvalues x = 2/8 {
		replace nsc_grad_fy`y' = 1 if grad_fy`x' == `y'
		foreach deg in assc bach mstr dctr {
			replace nsc_`deg'_fy`y' = `deg'`x' if grad_fy`x' == `y'
		}
	}
}
forvalues x = 0/$endx {
	g nsc_grad_in_`x' = nsc_grad_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9)
	g nsc_grad_by_`x' = nsc_grad_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9)
	g nsc_grad_postarr_by_`x' = nsc_grad_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9) & ym_arr < ym(2000,9)

	foreach deg in assc bach mstr dctr {
		gen nsc_`deg'_in_`x' = nsc_`deg'_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9)
		gen nsc_`deg'_by_`x' = nsc_`deg'_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9)
		gen nsc_`deg'_postarr_by_`x' = nsc_`deg'_fy2000 if (ym_arr + 3*(`x'-1)) >= ym(2000,9) & ym_arr < ym(2000,9)
		
	}
	forvalues y = 2001/2021 {
		replace nsc_grad_in_`x' = nsc_grad_fy`y' if (ym_arr + 3*(`x'-1)) >= ym(`y',9)
		replace nsc_grad_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_grad_fy`y' == 1
		replace nsc_grad_postarr_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_grad_fy`y' == 1 & ym_arr < ym(`y',9)
		foreach deg in assc bach mstr dctr {
			replace nsc_`deg'_in_`x' = nsc_`deg'_fy`y' if (ym_arr + 3*(`x'-1)) >= ym(`y',9)
			replace nsc_`deg'_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_`deg'_fy`y' == 1
			replace nsc_`deg'_postarr_by_`x' = 1 if (ym_arr + 3*(`x'-1)) >= ym(`y',9) & nsc_`deg'_fy`y' == 1 & ym_arr < ym(`y',9)
		}
	}
		foreach deg in grad assc bach mstr dctr {
			replace nsc_`deg'_in_`x' = 0 if missing(nsc_`deg'_in_`x')
			replace nsc_`deg'_by_`x' = 0 if missing(nsc_`deg'_by_`x')
			replace nsc_`deg'_postarr_by_`x' = 0 if missing(nsc_`deg'_postarr_by_`x')
			replace nsc_`deg'_in_`x' = 1 if nsc_`deg'_in_`x' > 1
			replace nsc_`deg'_by_`x' = 1 if nsc_`deg'_by_`x' > 1
			replace nsc_`deg'_postarr_by_`x' = 1 if nsc_`deg'_postarr_by_`x' > 1
		}
		
		gen nsc_asscplus_in_`x' = max(nsc_assc_in_`x',nsc_bach_in_`x',nsc_mstr_in_`x',nsc_dctr_in_`x')
		gen nsc_bachplus_in_`x' = max(nsc_bach_in_`x',nsc_mstr_in_`x',nsc_dctr_in_`x')
		gen nsc_mstrplus_in_`x' = max(nsc_mstr_in_`x',nsc_dctr_in_`x')
		
		gen nsc_asscplus_by_`x' = max(nsc_assc_by_`x',nsc_bach_by_`x',nsc_mstr_by_`x',nsc_dctr_by_`x')
		gen nsc_bachplus_by_`x' = max(nsc_bach_by_`x',nsc_mstr_by_`x',nsc_dctr_by_`x')
		gen nsc_mstrplus_by_`x' = max(nsc_mstr_by_`x',nsc_dctr_by_`x')
		gen nsc_asscplus_postarr_by_`x' = max(nsc_assc_postarr_by_`x',nsc_bach_postarr_by_`x',nsc_mstr_postarr_by_`x',nsc_dctr_postarr_by_`x')
		gen nsc_bachplus_postarr_by_`x' = max(nsc_bach_postarr_by_`x',nsc_mstr_postarr_by_`x',nsc_dctr_postarr_by_`x')
		gen nsc_mstrplus_postarr_by_`x' = max(nsc_mstr_postarr_by_`x',nsc_dctr_postarr_by_`x')
		}

forvalues x = 1/4 {
	g nsc_grad_in_m`x' = nsc_grad_fy2000 if (ym_arr + 3*(-`x'-1)) >= ym(2000,9)
	g nsc_grad_by_m`x' = nsc_grad_fy2000 if (ym_arr + 3*(-`x'-1)) >= ym(2000,9)

	forvalues y = 2001/2021 {
		replace nsc_grad_in_m`x' = nsc_grad_fy`y' if (ym_arr + 3*(-`x'-1)) >= ym(`y',9)
		replace nsc_grad_by_m`x' = 1 if (ym_arr + 3*(-`x'-1)) >= ym(`y',9) & nsc_grad_fy`y' == 1
	}

		}


foreach deg in grad assc bach mstr dctr {
	g nsc_`deg'_by_2017 = nsc_`deg'_fy2000
	g nsc_`deg'_by_2020 = nsc_`deg'_fy2000
	g nsc_`deg'_pa_by_2017 = nsc_`deg'_fy2000 if ym_arr < ym(2000,9)
	g nsc_`deg'_pa_by_2020 = nsc_`deg'_fy2000 if ym_arr < ym(2000,9)
forvalues y = 2001/2017 {
	replace nsc_`deg'_by_2017 = 1 if nsc_`deg'_fy`y' ==1 	
	replace nsc_`deg'_pa_by_2017 = 1 if nsc_`deg'_fy`y' ==1 & ym_arr < ym(`y',9)
}
forvalues y = 2001/2020 {
	replace nsc_`deg'_by_2020 = 1 if nsc_`deg'_fy`y' ==1 
	replace nsc_`deg'_pa_by_2020 = 1 if nsc_`deg'_fy`y' ==1 & ym_arr < ym(`y',9)
}
replace nsc_`deg'_by_2017 = 0 if missing(nsc_`deg'_by_2017)	
replace nsc_`deg'_by_2020 = 0 if missing(nsc_`deg'_by_2020)
replace nsc_`deg'_pa_by_2017 = 0 if missing(nsc_`deg'_pa_by_2017)	
replace nsc_`deg'_pa_by_2020 = 0 if missing(nsc_`deg'_pa_by_2020)
replace nsc_`deg'_by_2017 = 1 if nsc_`deg'_by_2017 > 1
replace nsc_`deg'_by_2020 = 1 if nsc_`deg'_by_2020 > 1	
replace nsc_`deg'_pa_by_2017 = 1 if nsc_`deg'_pa_by_2017 > 1
replace nsc_`deg'_pa_by_2020 = 1 if nsc_`deg'_pa_by_2020 > 1	

}
gen nsc_asscplus_by_2017 = max(nsc_assc_by_2017,nsc_bach_by_2017,nsc_mstr_by_2017,nsc_dctr_by_2017)
gen nsc_asscplus_by_2020 = max(nsc_assc_by_2020,nsc_bach_by_2020,nsc_mstr_by_2020,nsc_dctr_by_2020)
gen nsc_asscplus_pa_by_2017 = max(nsc_assc_pa_by_2017,nsc_bach_pa_by_2017,nsc_mstr_pa_by_2017,nsc_dctr_pa_by_2017)
gen nsc_asscplus_pa_by_2020 = max(nsc_assc_pa_by_2020,nsc_bach_pa_by_2020,nsc_mstr_pa_by_2020,nsc_dctr_pa_by_2020)

gen nsc_bachplus_by_2017 = max(nsc_bach_by_2017,nsc_mstr_by_2017,nsc_dctr_by_2017)
gen nsc_bachplus_by_2020 = max(nsc_bach_by_2020,nsc_mstr_by_2020,nsc_dctr_by_2020)
gen nsc_bachplus_pa_by_2017 = max(nsc_bach_pa_by_2017,nsc_mstr_pa_by_2017,nsc_dctr_pa_by_2017)
gen nsc_bachplus_pa_by_2020 = max(nsc_bach_pa_by_2020,nsc_mstr_pa_by_2020,nsc_dctr_pa_by_2020)

foreach x in grad enr assc bach mstr dctr asscplus bachplus {

	replace nsc_`x'_pa_by_2017 = 0 if nsc_`x'_pa_by_2017 == .
	replace nsc_`x'_pa_by_2020 = 0 if nsc_`x'_pa_by_2020 == .
	
}

	
*MERGE ADDITIONAL X
merge 1:1 pid using ${rawdataorig}/mepcom_last_unit_deploy_nopii, gen(_merge_mepcom) keep(1 3)

gen any_meps_med_fail_cd = mep_meps_med_fail_cd_1 != "" | mep_meps_med_fail_cd_2 != ""

gen any_meps_drug_tst_miss = 0
foreach drug in alcohol marijuana cocaine {
	gen any_meps_`drug'_fail = !inlist(mep_`drug'_test_result,"N","Z","")
	gen any_meps_`drug'_tst_miss = inlist(mep_`drug'_test_result,"Z","")
	replace any_meps_drug_tst_miss = 1 if inlist(mep_`drug'_test_result,"Z","")
}
gen any_meps_drug_fail = max(any_meps_alcohol_fail,any_meps_marijuana_fail,any_meps_cocaine_fail)


preserve
use ${rawdataorig}/ace_first_v2_unit_deploy_nopii, clear
foreach x in asvabap asvabcl asvabco asvabel asvabfa asvabgm asvabgt asvabmm asvabof asvabsc asvabtest ngibstat religcat statebrth filedt_s asvabdt_s ngibdt_s {
	rename `x' ace_first_`x'
}
tempfile ace_first
save `ace_first'
restore
merge 1:1 pid using `ace_first', nogen keep(1 3)

*GENs
g init_white = init_race == "W"
g init_black = init_race == "B"
g init_hispanic = init_race == "H"
g init_other_race = init_race == "X"
g init_afqsc_abv50 = init_afqsc >= 50 & init_afqsc != .
g init_afqsc_bel50 = init_afqsc < 50
g init_mos11 = substr(init_pmos_cons,1,2) == "11"
g combat_occ = substr(init_pmos_cons,1,2) == "11" | substr(init_pmos_cons,1,2) == "13" | substr(init_pmos_cons,1,2) == "14" | substr(init_pmos_cons,1,2) == "18" | substr(init_pmos_cons,1,2) == "19" | init_pmos_cons == "12B"
g noncombat_occ = !combat_occ
g hsd_ged = init_civedcatg == "HSD" | init_civedcatg == "GED"
g high_school = init_civedcatg == "HSG"
g college = init_civedcatg == "ASC" | init_civedcatg == "CLG" | init_civedcatg == "SMC" | init_civedcatg == "GRD"
g not_init_mos11 = substr(init_pmos_cons,1,2) != "11"
g early_cohort = inrange(year_arrival,2005,2010)
g late_cohort = inrange(year_arrival,2011,2015)


*------------------*
*Save data set with outcomes
*------------------*
keep pid sep_by_* wia_by_* kia_by_* anyvadc_in_* anyssi_in_* anyssdi_in_* amtdisability_in_* anydisability_in_* amtvadc_in_* num_qtr_allowed*  bar_or_misconduct_by_* black hisp other_race female married anydep hsgplus init_approx_age bct_type mos_post_qtr_terms init_pmos_cons post_orgraa init_terms dep_win_3yr count_inst_3yr insamp_inst_3_yr  init_white init_black init_hispanic init_other_race init_afqsc_abv50 init_afqsc_bel50 init_afqsc init_mos11 not_init_mos11 combat_occ noncombat_occ hsd_ged high_school college dmsl_tc_first afqs_mi qtr early_cohort late_cohort any_hfp_ever months_hfp_ever deployed_* year_arrival month_arrival BCTXarr* BCT months_hfp_3yr ndi* init_nrdep* anydep  ndi_war ndi_death_myr vantage_v3_score_in_* ficeclv9_score_in_* credit_coverage_in_*  credit_input_all credit_input_record vantage_score_in_* fico_score_in_* vantage_2007_2017 in_credit_samp_in_* vantage_6mo_score_in_yr* vantage_18mo_score_in_yr* vantage_30mo_score_in_yr* any_vantage in_credit_samp_6mo_in_yr* in_credit_samp_18mo_in_yr* in_credit_samp_30mo_in_yr* input_file_20* no_credit_score689 vantage_v3_score689 *premier_v1_2_*689* incarcerated_by_* incarcerated_in_* any_*profile_in_* any_*phys_in_* any_*hearing_in_* any_*psychiatric_in_* any_*profile_by_* any_*phys_by_* any_*hearing_by_* any_*psychiatric_by_* phypr_in_0 any_*othprof_in_* any_*othprof_by_* inc_by_*  post_state init_* mep_* any_mep* ace_first_* ap_m_eboa ap_m_ebtot ap_m_ebtp ap_m_srboa ap_m_srbtot ap_m_srbtp ap_filedt_s ap_mgibillstat _merge_mepcom  *p13_*731 *vantage_v4_score731 *fictbv10_score731 any_credit_file731 nsc_*_in_* nsc_*_by_*  combat_death*

compress
save "created_dta\analysis_samp_w_outcomes_incw", replace
